$(function () {
    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var client_datagrid =  $('#client_datagrid');
    var officialStudent_dialog = $("#officialStudent_dialog");
    var officialStudent_form = $("#officialStudent_form");

    //数据表格
    $('#client_datagrid').datagrid({
        url:'/client/list.do',
        title:"客户管理",
        striped:true,
        singleSelect:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',hidden:true,width:100},
            {field:'name',title:'客户名称',width:100},
            {field:'man',title:'性别',width:100,formatter:manFormatter},
            {field:'marketer',title:'营销人员',width:100,formatter:marketerFormatter},
            {field:'inputer',title:'录入人员',width:100,formatter:inputerFormatter},
            {field:'tracer',title:'跟踪人员',width:100,formatter:tracerFormatter},
            {field:'filingDate',title:'建档时间',width:100},
            {field:'inputTime',title:'录入时间',width:100},
            {field:'lastTraceTime',title:'最后跟踪时间',width:100},
            {field:'appointTime',title:'约访时间',width:100},
            {field:'qqNum',title:'QQ号',width:100},
            {field:'wechatNum',title:'微信号',width:100},
            {field:'tel',title:'电话',width:100},
            {field:'school',title:'学校',width:100},
            {field:'ittState',title:'意向程度',width:100,formatter:ittStateFormatter},
            {field:'campus',title:'意向校区',width:100,formatter:campusFormatter},
            {field:'ittSubject',title:'意向学科',width:100,formatter:ittSubjectFormatter},
            {field:'official',title:'是否正式客户',width:100,formatter:stateFormatter},
            {field:'remark',title:'备注',width:100}
        ]]
    });
    //end



    var cmdObj = {

        //将客户放入资源池,
        putPool : function(){

            var row = client_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要操作的客户","info");
                return;
            }

            $.messager.confirm('确认','是否要将当前客户放入资源池？',function(r){
                if (r){
                    $.get("/client/putPool.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','转移成功',function(r){
                                client_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },

        //查看按钮
        watch:function(){
            var row = client_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要查看的客户","info");
                return;
            }

            edit_dialog.dialog("setTitle","查看客户");

            //在读取数据之前,清除缓存
            edit_form.form("clear");

            $(".readOnly").textbox("readonly",true);

            if(row.campus){
                row["campus.id"]=row.campus.id;
            }


            if(row.source){
                row["source.id"]=row.source.id;
            }
            if(row.bigClientSchool){
                row["bigClientSchool.id"]=row.bigClientSchool.id;
            }
            if(row.territory){
                row["territory.id"]=row.territory.id;
            }
            if(row.education){
                row["education.id"]=row.education.id;
            }
            if(row.ittState){
                row["ittState.id"]=row.ittState.id;
            }
            if(row.ittSubject){
                row["ittSubject.id"]=row.ittSubject.id;
            }
            if(row.state){
                row["state.id"]=row.state.id;
            }

            if(row.tracer){
                row["tracer.id"]=row.tracer.id;
            }

            edit_form.form("load",row);

            $('.dialog-button a:eq(0)').hide();
            $('.dialog-button a:eq(1)').hide();

            edit_dialog.dialog("open");
        },

        //重置
        clear:function(){
            edit_form.form("clear");
        },

        query : function () {
            var param = $("#keyword").val();
            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();

            $('#client_datagrid').datagrid('load',{
                keyword: param,
                beginTime: beginTime,
                endTime: endTime
            });
        },

        //点击添加按钮,添加客户
        add : function () {
            edit_dialog.dialog("setTitle","添加客户");
            edit_form.form("clear");

            $('.dialog-button a:eq(0)').show();
            $('.dialog-button a:eq(1)').show();
            edit_dialog.dialog("open");
        },
        //刷新
        reload : function () {
            client_datagrid.datagrid("reload");
        },
        //编辑按钮编辑客户
        edit : function () {

            //显示按钮
            $('.dialog-button a:eq(0)').show();
            $('.dialog-button a:eq(1)').show();

            var row = client_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的客户","info");
                return;
            }

            edit_dialog.dialog("setTitle","编辑客户");

            //在读取数据之前,清除缓存
            edit_form.form("clear");


            if(row.campus){
                row["campus.id"]=row.campus.id;
            }


            if(row.source){
                row["source.id"]=row.source.id;
            }
            if(row.bigClientSchool){
                row["bigClientSchool.id"]=row.bigClientSchool.id;
            }
            if(row.territory){
                row["territory.id"]=row.territory.id;
            }
            if(row.education){
                row["education.id"]=row.education.id;
            }
            if(row.ittState){
                row["ittState.id"]=row.ittState.id;
            }
            if(row.ittSubject){
                row["ittSubject.id"]=row.ittSubject.id;
            }
            if(row.state){
                row["state.id"]=row.state.id;
            }
            if(row.tracer){
                row["tracer.id"]=row.tracer.id;
            }
            edit_form.form("load",row);

            edit_dialog.dialog("open");
        },
        //save按钮提交表单
        save : function () {

            var url = "";

            var id = $("#clientId").val();

            if(id){
                url = "/client/update.do";
            }else{
                url = "/client/save.do";
            }

            edit_form.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            client_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //删除客户
        remove : function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的客户","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/client/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                client_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        },
        //转正对话框关闭
        regularizationCancel : function (){
            officialStudent_dialog.dialog("close");
            officialStudent_dialog.form("clear");
        },
        //转正
        regularization : function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要转正的客户!","info");
                return;
            }
            //转正对话框打开
            officialStudent_dialog.dialog("open");
            //修改对话框标题
            officialStudent_dialog.dialog("setTitle","编辑正式学员");

            //营销人员
            if(row.marketingPerson){
                row["marketingPerson.id"] = row.marketingPerson.id;
            }
            //性别
            if(row.man == true){
                row["gender.id"] = 57;
            }else{
                row["gender.id"] = 58;
            }
            //学校客户
            if(row.schoolClient){
                row["schoolClient.id"] = row.schoolClient.id;
            }
            //学历
            if(row.education){
                row["education.id"] = row.education.id;
            }
            //客户来源
            if(row.clientSource){
                row["clientSource.id"] = row.clientSource.id;
            }
            //客户类型
            if(row.clientType){
                row["clientType.id"] = row.clientType.id;
            }
            //班级
            if(row.clazz){
                row["clazz.id"] = row.clazz.id;
            }
            //意向校区
            if(row.campus){
                row["campus.id"] = row.campus.id;
            }
            //就业意向
            if(row.jobIntention){
                row["jobIntention.id"] = row.jobIntention.id;
            }
            //学习状态
            if(row.studyState){
                row["studyState.id"] = row.studyState.id;
            }
            //支付方式
            if(row.paymentType){
                row["paymentType.id"] = row.paymentType.id;
            }

            //数据回显
            officialStudent_form.form("load",row);

        },
        //转正提交
        regularizationSave :function () {
            //提交表单
            officialStudent_form.form("submit", {
                url:"/officialStudent/save.do",
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示成功
                        $.messager.confirm('温馨提示',"操作成功",function(r){
                            //关闭窗口
                            officialStudent_dialog.dialog("close");
                            //刷新数据
                            client_datagrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert('温馨提示',data.msg, "error");
                    }
                }

            })
        }

    }

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end

    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: "80%",
        height: "80%"
    });
    //转正对话框初始化
    officialStudent_dialog.dialog({
        width: "80%",
        height: "80%",
        buttons:"#cc",
        closable:true,
        closed:true
    });

    //end


})//end




//营销人员格式化
function marketerFormatter(value,row,index) {
    if (value == null){
        return;
    }
    return value.username;
}
//跟踪人员格式化
function tracerFormatter(value,row,index) {

    if (value == null){
        return;
    }
    return value.username;
}
//录入人员格式化
function inputerFormatter(value,row,index) {

    if (value == null){
        return;
    }
    return value.username;
}
//意向程度格式化
function ittStateFormatter(value,row,index) {

    if (value == null){
        return;
    }
    return value.name;
}
//意向校区格式化
function campusFormatter(value,row,index) {

    if (value == null){
        return;
    }
    return value.name;
}
//意向学科格式化
function ittSubjectFormatter(value,row,index) {

    if (value == null){
        return;
    }
    return value.name;
}

//当前状态格式化
function stateFormatter(value,row,index) {
    return value ? "正式学员" : "普通客户";
}



//性别格式化
function manFormatter(value,row,index) {
    return value ? "男" : "女";
}

